<?php

/**
 *  经销商订单
*/

namespace app\api\controller;
use app\api\service\Order as ServiceOrder;
use app\facade\Basic;
use think\Request;
use app\api\service\DistributorOrder as ServiceDistributorOrder;

class DistributorOrder extends BaseController
{
    /**
     *  供应商列表
     */
    public function list(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;

        /* 页面 */
        $param['page'] = $request->param('page',1);
        $param['listRows'] = $request->param('listRows',15);

        /* 筛选 */
        $param['status'] = $request->param('status',null);
        $param['search_value'] = $request->param('search_value',null);

        /* 验证 */
        $this->validateData($param,'DistributorOrder.list');

        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->list($param);
        return Basic::json($result);
    }

    /**
     *  我的资产
    */
    public function assets(Request $request)
    {

        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['lang'] = $request->header('think-lang');

        /* 验证 */
       // $this->validateData($param,'DistributorOrder.assets');
        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->assets($param);
        return Basic::json($result);
    }


    /**
     *  发货结算页面
    */
    public function checkout(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['ids'] = $request->param('ids',null);
        $param['lang'] = $request->header('think-lang');

        /* 验证 */
        $this->validateData($param,'DistributorOrder.checkout');

        /* 发货结算页面 */
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->checkout($param);
        return Basic::json($result);
    }

    /**
     *  经销商 - 发货
    */
    public function create(Request $request)
    {

        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;


        $param['ids'] = $request->param('ids',null);
        $param['address_id'] = $request->param('address_id',null);
        $param['shipping_method_id'] = $request->param('shipping_method_id',null);
        $param['tax_package_id'] = $request->param('tax_package_id',null);
        $param['payment_method'] = $request->param('payment_method',null);
        $param['warehouse_code'] = $request->param('warehouse_code',null);

        /*  */
        $param['delivery_method'] = $request->param('delivery_method',null);  // 收货方式(0-自提 1-快递)
        $param['delivery_terms'] = $request->param('delivery_terms',null);   // 贸易条款(1-DDP 2-DDU)
        $param['prescription'] = $request->param('prescription',null);     // 1普快/2特快

        /* 支付方式 */
        $param['pay_terms'] = $request->param('pay_terms',0);   // 默认值 0全款 1预付款
        $param['advance_amount'] = $request->param('advance_amount',null);  // 默认值

        /* 账单地址 */
        $param['billing_address_id'] = $request->param('billing_address_id',null);  // 账单地址

        /* 自提 */
        $param['pickup_name'] = $request->param('pickup_name',null);  // 自提名称
        $param['pickup_phone'] = $request->param('pickup_phone',null);  // 自提电话
        $param['pickup_passport'] = $request->param('pickup_passport',null);  // 护照
        $param['pickup_vehicle'] = $request->param('pickup_vehicle',null);  // 车辆信息

        /* 备注 */
        $param['notes'] = $request->param('notes',null);  // 订单备注
        $param['owner'] = $request->param('owner',null);  // 订单备注

        /* 验证 */
        $this->validateData($param,'DistributorOrder.create');

        /* 创建订单 */
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->create($param);
        return Basic::json($result);
    }


    /**
     *  经销商 - 订单详情
    */
    public function orderDetail(Request $request)
    {

        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['order_no'] = $request->param('orderNo',null);
        $param['lang'] = $request->header('think-lang');

        /* 验证 */
        $this->validateData($param,'DistributorOrder.orderDetail');


        /* 获取订单详情 */
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->orderDetail($param);
        return Basic::json($result);

    }


    /**
     * 上传支付凭证
     */
    public function PaymentVoucher(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['orderNo'] = $request->param('orderNo',null);
        $param['file'] = $request->file('file');
        $param['notes'] = $request->param('notes',null);


        /* 验证 */
        $this->validateData($param,'order.payment_voucher');

        /* 获取订单详情 */
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->PaymentVoucher($param);
        return Basic::json($result);
    }

    /**
     *  删除删除凭证
     */
    public function VoucherRemove(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['id'] = $request->param('id',null);

        /* 验证 */
        $this->validateData($param,'order.voucher_remove');

        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->VoucherRemove($param);
        return Basic::json($result);
    }

    /**
     *  订单状态列表
     */
    public function getStatusList(Request $request)
    {
        $lang = $request->header('think-lang');
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->getStatusList($lang);
        return Basic::json($result);
    }


    /**
     *  拉起支付
     */
    public function payment(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['role'] = $request->role;
        $param['orderNo'] = $request->param('orderNo',null);
        $param['payment_method'] = $request->param('payment_method',null);
        $param['file'] = $request->file('file');
        $param['voucher_remark'] = $request->param('voucher_remark',null);

        /* 验证 */
        $this->validateData($param,'order.payment');

        /* 登录操作 */
        $ServiceDistributorOrder = new ServiceDistributorOrder();
        $result = $ServiceDistributorOrder->payment($param);
        return Basic::json($result);
    }
}
